$(document).ready(function(){
	/* This code is executed after the DOM has been completely loaded */

	var tmp;
	
	$('.note').each(function(){
		/* Finding the biggest z-index value of the notes */
		tmp = $(this).css('z-index');
		if(tmp>zIndex) zIndex = tmp;
	})
	
	
	
	/* Configuring the fancybox plugin for the "Add a note" button: */
	$("#addButton").fancybox({
		'height'		: 300,
		'autoDimensions': false,
		'zoomSpeedIn'		: 600,
		'zoomSpeedOut'		: 500,
		'easingIn'			: 'easeOutBack',
		'easingOut'			: 'easeInBack',
		'hideOnContentClick': false,
		'padding'			: 15
	});
	
	/* Listening for keyup events on fields of the "Add a note" form: */
	$('.pr-body,.pr-author').live('keyup',function(e){
		if(!this.preview)
			this.preview=$('#fancy_ajax .note');
		
		/* Setting the text of the preview to the contents of the input field, and stripping all the HTML tags: */
		this.preview.find($(this).attr('class').replace('pr-','.')).html($(this).val().replace(/<[^>]+>/ig,''));
	});
	
	/* Changing the color of the preview note: */
	$('.color').live('click',function(){
		$('#previewNote').removeClass('yellow green blue').addClass($(this).attr('class').replace('color',''));
	});
	
	
	
	//$('.note-form').live('submit',function(e){e.preventDefault();});
});


/*
* This was previously an inline function in the make_draggable function
* but with the addition of 'droppable' divs, it made more sense to move
* the functionality into the droppable's function, make_droppable().
*/
var zIndex = 0;
function save_position(x,y, id){
	/* Sending the z-index and positon of the note to update_position.php via AJAX GET: */
	$.get('functions/update_position.php',{
		x		: x,
		y		: y,
		z		: zIndex,
		id	: id,
		'pname' : project_name
	});
}

function make_draggable(elements){
	
	
	
	/* Elements is a jquery object: */
	
	elements.draggable({
		containment:'parent',
		revert:"invalid",
		start:function(e,ui){
			ui.helper.css('z-index',++zIndex);
			}
	});
	
	//these mouseup/down note bindings ensure that an update
	//will not occur when the user is dragging a note. If they
	//were not here, then the page would refresh while dragging
	//and the note would never be moved. 
	
	/* Visual aid that lets users know which card is being manipulated */
	$(".note").hover(
		//In
		function () {
			var project = $("#project_name").text();
			var cardID = $(this).find(".data").html();
			var color = "";
			if($(this).attr('id') != "previewNote"){
				//If card is yellow
				if($.trim($(this).attr("class").replace("note","").replace("ui-draggable","")) == "yellow" || $.trim($(this).attr("class").replace("note","").replace("yellow","").replace("ui-draggable","")) == "yH"){
					color = "yH";
				}
				//If card is blue
				else if($.trim($(this).attr("class").replace("note","").replace("ui-draggable","")) == "blue" || $.trim($(this).attr("class").replace("note","").replace("blue","").replace("ui-draggable","")) == "bH"){
					color = "bH";
				}
				//If cards is green
				else if($.trim($(this).attr("class").replace("note","").replace("ui-draggable","")) == "green" || $.trim($(this).attr("class").replace("note","").replace("green","").replace("ui-draggable","")) == "gH"){
					color = "gH";
				}
				
				$(this).addClass(color);
				console.log(color);
				$.post("functions/hoverOver.php", {"project":project, "color":color, "cardID":cardID});
			}
		},
		//Out
		function () {
			var project = $("#project_name").text();
			var cardID = $(this).find(".data").html();
			var color;
			var rmClass;
			if($(this).attr('id') != "previewNote"){
				//If card is yellow
				if($.trim($(this).attr("class").replace("note","").replace("yellow","").replace("ui-draggable","")) == "yH"){
					color = "yellow";
				}
				//If card is blue
				else if($.trim($(this).attr("class").replace("note","").replace("blue","").replace("ui-draggable","")) == "bH"){
					color = "blue";
				}
				//If cards is green
				else if($.trim($(this).attr("class").replace("note","").replace("green","").replace("ui-draggable","")) == "gH"){
					color = "green";
				}
				$(this).addClass(color);
				console.log(color);
				$.post("functions/hoverOver.php", {"project":project, "color":color, "cardID":cardID});
			}
		}
		
	);
	
	$(".note").mousedown(function(){	
		moving = true;
		clearTimeout(board_timeout);
	});
	
	$(".note").mouseup(function(){
		moving = false;
		startBoardRefresh();
	});
	
}
//This is no longer called here because it should not be called
//for chicken users. Instead, see function/get_notes.php. It checks
//tosee if the user is a chicken and then calls it in in return html.
  //make_draggable($(".note")); //call it

function updateNotePoints(projectName, divVal, cardID){
	var project = projectName;
	var val = divVal;
	var note = cardID;

	$.post("functions/update_notePoints.php", {"project":project, "val":val, "note":note});
}




/*
* Make droppable sets up the point divs (images on the board) to
* accept the notes as they are dropped over them. Once they are
* accepted, the drop: function is called (within this function) and
* the cards coordinates are changed to be positioned in the middle 
* of the div in the x direction only. 
*
* The y coordinate is passed to the save_position function (that stores
* it in the database) as y+1... there seems to be something going on
* between here and the database insertion that decreases the y value by
* 1, this is a hack until I understand why.
*
* This function deals with displaing the number of notes on each div.
*
* The function takes care of deleting cards as well.
*/
function make_droppable(elements){
	
	
	var project_name = $("#project_name").text();
	// An array for each of the droppable divs we have (excluding the trash bin)
	var arr0 = [];
	var arr1 = [];
	var arr2 = [];
	var arr3 = [];
	var arr4 = [];
	var arr5 = [];
	var arr6 = [];
	
	$.post("functions/get_div0ChildrenList.php", {"project":$("#project_name").text(), "div":"note_start"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a0.push(value);
		  console.log('a0: '+a0);
		});
		arr0 = a0[a0.length - 1];	
	});

	$.post("functions/get_div1ChildrenList.php", {"project":$("#project_name").text(), "div":"one_point_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a1.push(value);
		});
		console.log('a1: '+a1);	
		arr1 = a1[a1.length - 1];
	});
	
	$.post("functions/get_div2ChildrenList.php", {"project":$("#project_name").text(), "div":"two_points_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a2.push(value);
		});
		console.log('a2: '+a2);	
		arr2 = a2[a2.length - 1];
	});
	
	$.post("functions/get_div3ChildrenList.php", {"project":$("#project_name").text(), "div":"three_points_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a3.push(value);
		});
		console.log('a3: '+a3);	
		arr3 = a3[a3.length - 1];
	});
	
	$.post("functions/get_div4ChildrenList.php", {"project":$("#project_name").text(), "div":"five_points_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a4.push(value);
		});
		console.log('a4: '+a4);	
		arr4 = a0[a4.length - 1];
	});
	
	$.post("functions/get_div5ChildrenList.php", {"project":$("#project_name").text(), "div":"eight_points_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a5.push(value);
		});
		console.log('a5: '+a5);	
		arr5 = a5[a5.length - 1];
	});
	
	$.post("functions/get_div6ChildrenList.php", {"project":$("#project_name").text(), "div":"thirteen_points_main"}, function(data){
		var dSplit = data.split(",");
		$.each(dSplit, function(index, value) {  
		  a6.push(value);
		});
		console.log('a6: '+a6);	
		arr6 = a6[a6.length - 1];
	});
	
	
	elements.droppable({
		accept: ".note",
		drop: function(event, ui){
			var pos = parseInt($(this).offset().left);
			var offset = parseInt($(this).width()-10);
			var x = (pos - offset)
			var y = ui.draggable.position().top;
			ui.draggable.css( {"left": x , "top":y});
			save_position(x, y+1, ui.draggable.find(".data").html() );

			var divID = $(this).attr('id');
			var divVal = $(this).find('span[class*="size"]').text();
			var cardID = ui.draggable.find(".data").html();
		
			// Update note points
			updateNotePoints(project_name, divVal, cardID);

			/* If the note is dropped in the INITIAL PILE DIV*/
			if(divID == "note_start"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr0) == -1){
					arr0.push(ui.draggable.find(".data").html());
					idS = $.unique(arr0);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}	
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr0) > -1){
					idS = $.unique(arr0);
				}
				$('.start_kids').text(idS.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the FIRST DIV*/
			if(divID == "one_point_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr1) == -1){
					arr1.push(ui.draggable.find(".data").html());
					idS = $.unique(arr1);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}	
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr1) > -1){
					idS = $.unique(arr1);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(idS.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the SECOND DIV*/
			if(divID == "two_points_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr2) == -1){
					arr2.push(ui.draggable.find(".data").html());
					idS = $.unique(arr2);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr2) > -1){
					idS = $.unique(arr2);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(idS.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the THIRD DIV*/
			if(divID == "three_points_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr3) == -1){
					arr3.push(ui.draggable.find(".data").html());
					idS = $.unique(arr3);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}	
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr3) > -1){
					idS = $.unique(arr3);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(idS.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the FOURTH DIV*/
			if(divID == "five_points_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr4) == -1){
					arr4.push(ui.draggable.find(".data").html());
					idS = $.unique(arr4);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}	
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr4) > -1){
					idS = $.unique(arr4);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(idS.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the FIFTH DIV*/
			if(divID == "eight_points_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr5) == -1){
					arr5.push(ui.draggable.find(".data").html());
					idS = $.unique(arr5);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr6) > -1){
						arr6.splice(jQuery.inArray(cardID, arr6),1);
					}		
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr4) > -1){
					idS = $.unique(arr4);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(idS.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the SIXTH DIV*/
			if(divID == "thirteen_points_main"){
				var idS;
				//If note is not in the list
				if(jQuery.inArray(cardID, arr6) == -1){
					arr6.push(ui.draggable.find(".data").html());
					idS = $.unique(arr6);
					//Check where the note was previously and remove it from the list
					if(jQuery.inArray(cardID, arr0) > -1){
						arr0.splice(jQuery.inArray(cardID, arr0),1);
					}
					else if(jQuery.inArray(cardID, arr1) > -1){
						arr1.splice(jQuery.inArray(cardID, arr1),1);
					}
					else if(jQuery.inArray(cardID, arr2) > -1){
						arr2.splice(jQuery.inArray(cardID, arr2),1);
					}
					else if(jQuery.inArray(cardID, arr3) > -1){
						arr3.splice(jQuery.inArray(cardID, arr3),1);
					}
					else if(jQuery.inArray(cardID, arr4) > -1){
						arr4.splice(jQuery.inArray(cardID, arr4),1);
					}
					else if(jQuery.inArray(cardID, arr5) > -1){
						arr5.splice(jQuery.inArray(cardID, arr5),1);
					}	
				}
				//If note is already in the list
				else if(jQuery.inArray(cardID, arr4) > -1){
					idS = $.unique(arr4);
				}
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(idS.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
			
			/* If the note is dropped in the TRASH*/
			if(divID == "trash"){
				//Delete the note
				if(confirm("Are you sure you want to delete this story?")){
					var id = ui.draggable.find(".data").html();
					$.post("functions/delete_notes.php", {"project":project_name, "id":id});
				}
				//Check where the note was previously and remove it from the list
				if(jQuery.inArray(cardID, arr0) > -1){
					arr0.splice(jQuery.inArray(cardID, arr0),1);
				}
				else if(jQuery.inArray(cardID, arr1) > -1){
					arr1.splice(jQuery.inArray(cardID, arr1),1);
				}
				else if(jQuery.inArray(cardID, arr2) > -1){
					arr2.splice(jQuery.inArray(cardID, arr2),1);
				}
				else if(jQuery.inArray(cardID, arr3) > -1){
					arr3.splice(jQuery.inArray(cardID, arr3),1);
				}
				else if(jQuery.inArray(cardID, arr4) > -1){
					arr4.splice(jQuery.inArray(cardID, arr4),1);
				}
				else if(jQuery.inArray(cardID, arr5) > -1){
					arr5.splice(jQuery.inArray(cardID, arr5),1);
				}
				else if(jQuery.inArray(cardID, arr6) > -1){
					arr6.splice(jQuery.inArray(cardID, arr6),1);
				}
				
				$('.start_kids').text(arr0.length); // display # of notes in div 0
				$('.one_kids').text(arr1.length); // display # of notes in div 1
				$('.two_kids').text(arr2.length); // display # of notes in div 2
				$('.three_kids').text(arr3.length); // display # of notes in div 3
				$('.five_kids').text(arr4.length); // display # of notes in div 4
				$('.eight_kids').text(arr5.length); // display # of notes in div 5
				$('.thirteen_kids').text(arr6.length); // display # of notes in div 6
				
				//Update 'points' table
				$.post("functions/update_divInfo.php", {"project_name":project_name, "note_start_kids":arr0, "one_point_main_kids":arr1, "two_points_main_kids":arr2, "three_points_main_kids":arr3, "five_points_main_kids":arr4, "eight_points_main_kids":arr5, "thirteen_points_main_kids":arr6});
			}
			
		}
	});
}
make_droppable($(".droppable")); //call it


/*
* Starts a timeout that sends a post request to refresh
* the participants pane on the left side. 
* This function is called inside of a <scipt> tag that is 
* pulled down from get_users.php, and thus is executed every
* post, creating an update-interval effect. 
*
* The timeout ID is returned in the function in to allow it
* to be canceled.
*/
function startUserRefresh(){
	return setTimeout(function(){
		refreshUsers();
	}, 2000);
}

/*
* Similar to startUserRefresh() above, but with the game board.
* This is inserted in a <scipt> tag in get_notes.php to create
* an intervalled refresh. 
*
* This is slightly different in that it must be stopped when a
* note is moving or the post will interfere with the user's movement.
* The 'moving' bool is set to false when a card is picked up to 
* circumvent this, as well as the timeout being canceled. It is
* restarted when the card is placed. 
*/
var moving = false;
function startBoardRefresh(){
	return setTimeout(function(){
		if(!moving)
			refreshBoard();
	}, 2000);
}

/*
* Just like the above two functions, except it is responsible for
* refreshing the chat window.
*/
function startChatRefresh(){
	return setTimeout(function(){
		refreshChat();
	}, 2000);
}

/*
* Refresh div counters	
*/
function startDiv0CounterRefresh(){
	return setTimeout(function(){
		refreshDiv0Children();
	}, 2000);
}

function startDiv1CounterRefresh(){
	return setTimeout(function(){
		refreshDiv1Children();
	}, 2000);
}

function startDiv2CounterRefresh(){
	return setTimeout(function(){
		refreshDiv2Children();
	}, 2000);
}

function startDiv3CounterRefresh(){
	return setTimeout(function(){
		refreshDiv3Children();
	}, 2000);
}

function startDiv4CounterRefresh(){
	return setTimeout(function(){
		refreshDiv4Children();
	}, 2000);
}

function startDiv5CounterRefresh(){
	return setTimeout(function(){
		refreshDiv5Children();
	}, 2000);
}

function startDiv6CounterRefresh(){
	return setTimeout(function(){
		refreshDiv6Children();
	}, 2000);
}

/*
* Refresh div points
*/
function startDiv1PointsRefresh(){
	return setTimeout(function(){
		refreshDiv1Points();
	}, 2000);
}

function startDiv2PointsRefresh(){
	return setTimeout(function(){
		refreshDiv2Points();
	}, 2000);
}

function startDiv3PointsRefresh(){
	return setTimeout(function(){
		refreshDiv3Points();
	}, 2000);
}

function startDiv4PointsRefresh(){
	return setTimeout(function(){
		refreshDiv4Points();
	}, 2000);
}

function startDiv5PointsRefresh(){
	return setTimeout(function(){
		refreshDiv5Points();
	}, 2000);
}

function startDiv6PointsRefresh(){
	return setTimeout(function(){
		refreshDiv6Points();
	}, 2000);
}


/*
* Send a chat onte to the server. The 'user' and 'project_name' 
* variables were created in the index.php and project/index.php
* files inside of script tags, inserted by php.
*/
$("#chatBar").keyup(function(e){
	if(e.keyCode == 13){
		$.post("functions/add_chat.php", {"project":project_name, "message":$("#chatBar").val(), "username":user}, function(data){

			setTimeout( '$("#chatText").scrollTop(1000000)',500 ); //need to find the max scroll height
			//if we ever need to do anything here
		});
		$(this).val("");
	}
});


/*
* Display selected points
*/
function points(selection, display){
	var selection = selection;
	var display = display;
	var project_name = $("#project_name").text();
	var grandpa = $(selection).parent().parent().attr('id');
	$(selection).change(function() {
		var option = this.options[this.selectedIndex];
		var val;
		//If no numbr is selected display nothing
		if($(option).text() == "Size"){
			$(display).text('');
			val = '';
		}
		//If a number is selected post data to update_divPoints and display the selected option
		else{
			$(display).text($(option).text());
			val = $(option).text();
		}
		//Update 'points' table
		$.post("functions/update_divPoints.php", {"project_name":project_name, "grandpa":grandpa,"divVal":val});
		
	});
}

/*
* These function calls start the cascade of automatic refreshes
* by pulling down the user and board panes from get_notes.php and 
* get_users.php. Remember, there is no setInterval being used, the 
* functions are just called again from within <script> tags in the 
* post response.
*/
refreshBoard();
refreshUsers();
refreshChat();

refreshDiv0Children();
refreshDiv1Children();
refreshDiv2Children();
refreshDiv3Children();
refreshDiv4Children();
refreshDiv5Children();
refreshDiv6Children();

refreshDiv1Points();
refreshDiv2Points();
refreshDiv3Points();
refreshDiv4Points();
refreshDiv5Points();
refreshDiv6Points();


